package com.student.xujiang.myallstudent.glide;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.student.xujiang.R;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by XD on 2017/3/6.
 */

public class GlideRecyclerviewAdapter extends RecyclerView.Adapter<GlideRecyclerviewAdapter.ViewHolder> {


    private Context mContext;
    /*
    * 准备数据
    * */
    String[] mDatas = new String[]{
            "http://img1.imgtn.bdimg.com/it/u=388521019,3418915482&fm=21&gp=0.jpg",
            "http://img1.imgtn.bdimg.com/it/u=2748683686,3232174417&fm=21&gp=0.jpg",
            "http://img5.imgtn.bdimg.com/it/u=3909823983,3753476484&fm=21&gp=0.jpg",
            "http://img5.imgtn.bdimg.com/it/u=2373549711,3835726044&fm=21&gp=0.jpg",
            "http://img4.imgtn.bdimg.com/it/u=2284882479,1854889009&fm=21&gp=0.jpg",
            "http://img4.imgtn.bdimg.com/it/u=838633181,239721683&fm=23&gp=0.jpg",
            "http://img2.imgtn.bdimg.com/it/u=1249704742,3875823998&fm=23&gp=0.jpg",
            "http://img1.imgtn.bdimg.com/it/u=3009713972,3252974559&fm=23&gp=0.jpg",
            "http://img0.imgtn.bdimg.com/it/u=236384210,2772634264&fm=23&gp=0.jpg",
            "http://img5.imgtn.bdimg.com/it/u=1007840712,574002593&fm=23&gp=0.jpg",
            "http://img4.imgtn.bdimg.com/it/u=2312130340,2627943976&fm=23&gp=0.jpg",
            "http://img4.imgtn.bdimg.com/it/u=2929362257,2343491229&fm=23&gp=0.jpg",
            "http://img3.imgtn.bdimg.com/it/u=1601128091,2659176880&fm=23&gp=0.jpg",
            "http://img5.imgtn.bdimg.com/it/u=19670328,688703177&fm=23&gp=0.jpg",
            "http://img0.imgtn.bdimg.com/it/u=2657893920,4044129460&fm=23&gp=0.jpg",
    };

    public GlideRecyclerviewAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemview = View.inflate(mContext, R.layout.item_glide_recyclerview, null);
        return new ViewHolder(itemview);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //获取数据

        //显示数据
        int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, mContext.getResources().getDisplayMetrics());
        int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200f, mContext.getResources().getDisplayMetrics());

        Glide.with(mContext)
                .load(mDatas[position])
                .placeholder(R.mipmap.ic_launcher)  //占位图
                .error(R.mipmap.ic_launcher) //出错占位图
                .override(width, height) //图片显示分辨率 像素值 可以转化为dp再设置
                .animate(R.anim.glide_anim)  //动画显示
                .centerCrop()  //显示样式
                .fitCenter()
                .into(holder.image);
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.length;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_glide_recyclerview)
        ImageView image;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}
